$(document).ready(function(){
	$(".tabLi").click(function(){
		clickTab($(this));
	});
});

function clickTab(tab){
	var old = $(".highlighted");
	if($(old).attr("id") != $(tab).attr("id")){
		$(old).removeClass("highlighted");
		$(old).addClass("faded");
		$(tab).removeClass("faded");
		$(tab).addClass("highlighted");
		if($(tab).attr("id")=="entitybmList"){
			$(".datasetbmList").hide();
			$(".entitybmList").show();
			$("#currTab").val() = "0";
		}else if($(tab).attr("id")=="datasetbmList"){
			if($(".datasetbmList table tbody").children().length<=0){
				getDatasetBmList();
			}
			$(".entitybmList").hide();
			$(".datasetbmList").show();
			$("#currTab").val() = "1";
		}
	}
}
function getDatasetBmList(){
//	$(".datasetbmList table tbody").addClass("loading2");
	$(".tableDiv").addClass("loading2");
	var data={};
	data["action"]="getDatasetbmList";
	data["lang"]= $("#display-lang").val();
	data["user"]=$(".uinfo").text().trim();
	data = JSON.stringify(data);
	data = ("msg="+encodeURIComponent(data));
	$.ajax({
		url: WEBAPP+'dsBookmarkManageServlet',//'groupDetailServlet',
		dataType:'json',
		type:'post',
		data:data,
		success:function(result){
//			$(".datasetbmList table tbody").removeClass("loading2");
			$(".tableDiv").removeClass("loading2");
			
//			$(".datasetbmList table tbody").append(text);
			var array = result["result"];
			for(var i=0; i<array.length; i++){
				var element = array[i];
				var tr = $("<tr></tr>");
				$(".datasetbmList table tbody").append(tr);
				tr.attr("id", element["bmid"]);
				var td1 = $("<td></td>");
				td1.addClass("select")
				var select = $("<input type='checkbox' class='checkbox'/>");
				td1.append(select);
				var td2 = $("<td></td>");
				td2.addClass("bmname");
				td2.text(element["bmname"]);
				var td3 = $("<td></td>");
				var a = $("<a></a>");
				var href;
				if(element["ds_type"] == "custom"){
					href = WEBAPP+"dataset/c"+element["dsid"];
				}else if(element["ds_type"] == "uri"){
					href = WEBAPP+"dataset/u"+element["dsid"];
				}
				a.attr("href", href);
				a.append(element["dsname"]);
				td3.append(a);
				tr.append(td1);
				tr.append(td2);
				tr.append(td3);
			}
		}
	});
}
function delEBM(btn){
	var ch = $("table input:checked");
	var flag = true;
	var displayLang = $("#display-lang").val();
	if(ch.length>0){
		var bmIds = "";
		var bmNames = "";
		ch.each(function(){
//			alert("|"+$(".uinfo").text().trim()+"|"+$(this).parent().next().text()+"|");
			bmIds += $(this).parent().parent().attr("id")+",";
			bmNames += $(this).parent().next().text()+",";
		});
		if(flag){
			bmIds = bmIds.substring(0, bmIds.length-1);
			bmNames = bmNames.substring(0, bmNames.length-1);
			var showText;
			if(displayLang == 'en'){
				showText = "Are you sure to delete bookmark(s) "+bmNames+" ?";
			}else if(displayLang == 'zh'){
				showText = "是否确定要删除收藏： "+bmNames+"?";
			}
			if(confirm(showText)){
				var data={};
				data["action"]="delEBM";
				data["bmIds"]=bmIds;
				data = JSON.stringify(data);
				data = ("msg="+encodeURIComponent(data));
				$.ajax({
					url:WEBAPP+'eBookmarkManageServlet',//'groupDetailServlet',
					dataType:'text',
					type:'post',
					data:data,
					success:function(text){
						var displayLang = $("#display-lang").val();
						if(text == "success"){
							if(displayLang == 'en'){
								alert("deleted!");
							}else if(displayLang == 'zh'){
								alert("已删除!");
							}
							$(".checkbox").removeAttr("checked");
						}
						else{
							if(displayLang == 'en'){
								alert("Failed, please try latter!");
							}else if(displayLang == 'zh'){
								alert("操作失败，请稍后再试!");
							}
						}
						window.location.reload();
					}
				});
			}
		}
	}else{
		if(displayLang == 'en'){
			alert("please select a member!");
		}else if(displayLang == 'zh'){
			alert("请选择要删除的收藏!");
		}
		
	}
}

function delDSBM(btn){
	var ch = $("table input:checked");
	var flag = true;
	var displayLang = $("#display-lang").val();
	if(ch.length>0){
		var bmIds = "";
		var bmNames = "";
		ch.each(function(){
//			alert("|"+$(".uinfo").text().trim()+"|"+$(this).parent().next().text()+"|");
			bmIds += $(this).parent().parent().attr("id")+",";
			bmNames = $(this).parent().next().text()+",";
		});
		if(flag){
			bmIds = bmIds.substring(0, bmIds.length-1);
			bmNames = bmNames.substring(0, bmNames.length-1);
			var showText;
			if(displayLang == 'en'){
				showText = "Are you sure to delete bookmark(s) "+bmNames+" ?";
			}else if(displayLang == 'zh'){
				showText = "是否确定要删除收藏： "+bmNames+"?";
			}
			if(confirm(showText)){
				var data={};
				data["action"]="delEBM";
				data["bmIds"]=bmIds;
				data = JSON.stringify(data);
				data = ("msg="+encodeURIComponent(data));
				$.ajax({
					url:WEBAPP+'dsBookmarkManageServlet',//'groupDetailServlet',
					dataType:'text',
					type:'post',
					data:data,
					success:function(text){
						var displayLang = $("#display-lang").val();
						if(text == "success"){
							if(displayLang == 'en'){
								alert("deleted!");
							}else if(displayLang == 'zh'){
								alert("已删除!");
							}
						}
						else{
							if(displayLang == 'en'){
								alert("Failed, please try latter!");
							}else if(displayLang == 'zh'){
								alert("操作失败，请稍后再试!");
							}
						}
						var location = window.location.href+"?select='1'";
						window.location.href = location;
					}
				});
			}
		}
	}else{
		if(displayLang == 'en'){
			alert("please select a member!");
		}else if(displayLang == 'zh'){
			alert("请选择要删除的收藏!");
		}
		
	}
}